window.addEventListener('load', function () {

    //获取数据
    showPro();
    function showPro() {
        //获取传过来的id
        const url = new URL(window.location.href);
        var id = url.searchParams.get('id');
        // console.log(id);
        //大图
        var detailShow = document.querySelector('.detail-show');
        //名称
        var title = document.querySelector('.proInfo-title');
        //描述
        var subTitle = document.querySelector('.proInfo-subTitle');
        //价格
        var price = document.querySelector('.priceNum');
        //规格
        var spec = document.querySelector('.spec-value');
        //积分
        var integral = document.querySelector('.integral-num');



        if (window.XMLHttpRequest) {
            var xhr = new XMLHttpRequest();

        }
        xhr.open('get', 'http://localhost:3000/product?id=' + id, false);
        xhr.send();
        if (xhr.status == 200 && xhr.readyState == 4) {
            var data = JSON.parse(xhr.responseText);
            // console.log(data[0]);
            // console.log(data[0].name);
            detailShow.innerHTML = `<img src=${data[0].bigImg} alt="">`;
            title.innerHTML = data[0].name;
            subTitle.innerHTML = data[0].des;
            price.innerHTML = data[0].price;
            spec.innerHTML = data[0].size;
            integral.innerHTML = data[0].integral;
        }
    }
    //点击加入购物车
    buyNum();
    function buyNum() {
        var removeN = document.querySelector('.num-value .remove');
        var addN = document.querySelector('.num-value .add');
        var number = document.querySelector('.num-value .number');
        var buyBtn = document.querySelector('.buyButton');
        var buyNmuber = number.value
        removeN.onclick = function () {
            if (buyNmuber > 1) {
                buyNmuber--;
            } else {
                buyNmuber = 1
            }
            number.value = buyNmuber;

        }
        addN.onclick = function () {
            buyNmuber++;
            number.value = buyNmuber;
        }

        buyBtn.onclick = function () {
            if (!number.value) {
                number.value = 1;
                alert('请选择购买数量')
            } else {
                window.location.href='userInfo.html';
                //获取用户id
                var uid = localStorage.getItem('uid');
                //获取商品id
                const url = new URL(window.location.href);
                var productId = url.searchParams.get('id');

                //判断购物车中有没有该商品
                // 1.根据用户名获取该用户所有购物车
                if (window.XMLHttpRequest) {
                    var xhr = new XMLHttpRequest();
                }
                xhr.open('get', 'http://localhost:3000/cars?uId=' + uid, false);
                xhr.send();
                if (xhr.status == 200 && xhr.readyState == 4) {
                    var flag = false
                    var data = JSON.parse(xhr.responseText);
                    if (data.length > 0) {
                        //2.循环购物车中所有商品id判断该商品是否存在
                        for (var i = 0; i < data.length; i++) {
                            if (productId == data[i].productId) {
                                //存在
                                flag = true;
                                var carId=data[i].id
                                buyNmuber=parseFloat(buyNmuber)+parseFloat(data[i].count)
                                console.log(buyNmuber);
                            }
                        }
                    }
                }
                // 判断flag的值
                if(flag){
                    upCars(carId,uid,productId,buyNmuber)
                }else{
                    addCars(uid,productId,buyNmuber)
                }


                //修改购物车数量(要修改的购物车的id,用户的id,商品的id要修改的数量)
                function upCars(carId,uid,productId, count) {
                    if (window.XMLHttpRequest) {
                        var xhr = new XMLHttpRequest();
                    }
                    xhr.open('put', 'http://localhost:3000/cars/' + carId, false);
                    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                    xhr.send(`uId=${uid}&productId=${productId}&count=${count}`);
                    if (xhr.status == 200 && xhr.readyState == 4) {
                        var data = JSON.parse(xhr.responseText);
                    }
                    console.log('修改了一条数据'+count);
                }

                //添加一条购物车
                function addCars(uid, proId, count) {
                    console.log(`uId=${uid}&productId=${proId}&count=${count}`);
                    if (window.XMLHttpRequest) {
                        var xhr = new XMLHttpRequest();
                    }
                    xhr.open('post', 'http://localhost:3000/cars', false);
                    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                    xhr.send(`uId=${uid}&productId=${productId}&count=${count}`);
                    if (xhr.status == 200 && xhr.readyState == 4) {
                        var data = JSON.parse(xhr.responseText);
                    }
                    console.log('添加了一条数据');
                }


            }
        }
    }

})

